<?php
/* Copyright (c) 2008 Hailey Bannister
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
    * Neither the name of the aframephp project nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

 */


if (file_exists('config.php')){
	include_once('config.php');	
} else {
	die ('No config file found..');
}

if(!defined('IS_CONFIGURED')) {
                die ('You should configure your install properly..');
}

//Global settings that generally wont ever need to change..

$afDefaultPage = 'main'; //The default page the proxy will load if $_GET['req' is set..



if(!isset($caTimeout)) {
    $caTimeout = 14400; //Default cache time of 4 hours.
}

//$cnt is used to keep count of the amount of debug msg's we have sent
$cnt = 0;

$gen_date = date("YmdHis");
$afdebug  = @array('server' => $_SERVER['SERVER_NAME'],
				 'self' => $_SERVER['PHP_SELF'],
				 'gen-date' => $gen_date);


//Our autoload feature..
function __autoload($class_name) {
  global $path;
		//Well lets look for a file in our path...
		//I had to do this because sometimes it would error on failed loads of classes..
		//And I figure it may be safer this way...
	foreach ($path as $dir) {
		if(file_exists($dir .'/'. $class_name . '.mod.php')){
		 require_once  $class_name . '.mod.php';
		 return;
	  }
  }
}

/* Lets load memcache! */
function memOn() {
    $cache = new Memcache;
    //lets load memcache servers.
    global $memcache_servers;
    
    foreach ($memcache_servers as $server) {
	    $serverinfo = explode(":",$server);
	    $cache->addServer($serverinfo[0],$serverinfo[1]);
    }
    $primary_memcache = explode(":",$memcache_servers[0]);
    //Now lets connect to our servers..
    if (!$cache->connect($primary_memcache[0],$primary_memcache[1])){
	    debug ("Fatal error: Could not connect to servers");
    }
    return $cache;
}

function dbOn() {
    global $sql_conf;
    $db = new sql($sql_conf);
    return $db;
}

//Sometimes this function dosn't exist.
if(!function_exists('hash')) {
   function hash($algo, $data, $raw_output = 0)
   {
      if($algo == 'md5') return(md5($data, $raw_output));
      else return(sha1($data, $raw_output));
   }
}

//debug function, print's msg and if severity is set then it always
//displays the message
function debug ($msg, $severity = 1){
	global $debug,$html_comments,$cnt,$minSev;
	$cnt++;
	if ($debug == -1)
		return -1;

    if ($severity < $minSev){
        return -1;
    }

	if($html_comments == 1) {
		echo "<!--msg[$cnt]: ";
		$msg = htmlspecialchars($msg);
	}

    switch ($severity) {
        case '1':
            $sev = 'e';
            break;
        case '2':
            $sev = 'f';
            break;
        default:
            $sev = 'w';
            break;
    }

	if(isset($_GET['debug']) OR $debug == 1){
		echo " [{$sev}$cnt]";
		print_r($msg);
	} 
		if($html_comments == 1)
			echo "-->";
		else
			echo " ";

}
?>
